/*
 * Engine TEST
 * danouvrs@gmail.com
 * You have no right on this file
 */

package engine.window;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Frame;

public class Window {

    private Frame frame;
    private GraphicsPane graphicsPane;
    //private FullScreen fullscreen;
    //private Keyboard keyboard;
    
    private int width;
    private int height;
    private final String TITLE;
    //private final boolean STARTFULLSCREEN;

    public Window(String title, int wWidth, int wHeight, boolean visibility) {
        width = wWidth;
        height = wHeight;
        TITLE = title;
        //STARTFULLSCREEN = startFS;
        
        frame = new Frame();
        frame.setTitle(TITLE);
        frame.setSize(width, height);
        frame.setUndecorated(false);
        frame.setLocationRelativeTo(null);
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                exit();
            }
            
            @Override
            public void windowActivated(WindowEvent e){
                if(graphicsPane != null)
                    graphicsPane.getGLCanvas().requestFocus();
                else
                    frame.requestFocus();
            }
        });
        System.out.println("Window initilized");
        
        //fullscreen = new FullScreen();
        
        graphicsPane = new GraphicsPane(frame);
        /*
        if(graphicsPane == null){
            keyboard = new Keyboard(this, fullscreen);
            frame.addKeyListener(keyboard);
        }
        else{
            keyboard = new Keyboard(this, graphicsPane, fullscreen);
            frame.add(graphicsPane.getGLCanvas());
            graphicsPane.getGLCanvas().addKeyListener(keyboard);
        }
        */
        frame.setVisible(true);
        
        //if(STARTFULLSCREEN)
        //    fullscreen.switchMode(frame, graphicsPane.getFPSAnimator(), graphicsPane.getGLCanvas());
        
    }

    public Frame getFrame() {
        return frame;
    }
    
    public GraphicsPane getGraphicsPane() {
        return graphicsPane;
    }

    public void exit() {
        System.out.println("Prepare to close...");
        
        if(graphicsPane != null){
            if(graphicsPane.FPSAnimatorStop()){
                System.out.println("Closing FPSAnimator\t\t\t[OKAY]");
            }
            else{
                System.out.println("Closing FPSAnimator\t\t\t[ERROR]");
            }
        }
        
        if(WindowStop()){
            System.out.println("Closing Window\t\t\t\t[OKAY]");
        }
        else{
            System.out.println("Closing Window\t\t\t\t[ERROR]");
        }
        
        /*if (getVerbose()) {
            showAllThreads();
        }*/
        
        System.exit(0);
    }
    
    public boolean isFrameActive(){
        return this.getFrame().isVisible();
    }

    private void showAllThreads() {
        ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
        ThreadGroup parentGroup;
        while ((parentGroup = rootGroup.getParent()) != null) {
            rootGroup = parentGroup;
        }
        Thread[] threads = new Thread[rootGroup.activeCount()];
        while (rootGroup.enumerate(threads, true) == threads.length) {
            threads = new Thread[threads.length * 2];
        }

        for (int i = 0; i < threads.length; i++) {
            if (i == 10 || i == 11) {
                //threads[i].interrupt();
            }
            if (threads[i] != null && true) {
                System.out.println("[" + threads[i].getName() + "][" + threads[i].getClass() + "](" + threads[i].getState() + ")");
                //threads[i].interrupt();
            }

        }
    }
    
    public boolean WindowStop(){
        frame.dispose();
        return true;
    }
}
